home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / gud.el < prev    next >
Lisp/Scheme  |  1993-06-30  |  35KB  |  948 lines

  1. ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
  2. ;;;            under Emacs
  3.  
  4. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  5. ;; Version: 1.3
  6. ;; Keywords: unix, tools
  7.  
  8. ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  24. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
  29. ;; It was later rewritten by rms.  Some ideas were due to Masanobu. 
  30. ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
  31. ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
  32. ;; who also hacked the mode to use comint.el.  Shane Hartman <shane@spr.com>
  33. ;; added support for xdb (HPUX debugger).
  34.  
  35. ;;; Code:
  36.  
  37. (require 'comint)
  38. (require 'etags)
  39.  
  40. ;; ======================================================================
  41. ;; GUD commands must be visible in C buffers visited by GUD
  42.  
  43. (defvar gud-key-prefix "\C-x\C-a"
  44.   "Prefix of all GUD commands valid in C buffers.")
  45.  
  46. (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
  47. (global-set-key "\C-x " 'gud-break)    ;; backward compatibility hack
  48.  
  49. ;; ======================================================================
  50. ;; the overloading mechanism
  51.  
  52. (defun gud-overload-functions (gud-overload-alist)
  53.   "Overload functions defined in GUD-OVERLOAD-ALIST.
  54. This association list has elements of the form
  55.      (ORIGINAL-FUNCTION-NAME  OVERLOAD-FUNCTION)"
  56.   (mapcar
  57.    (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
  58.    gud-overload-alist))
  59.  
  60. (defun gud-debugger-startup (file args)
  61.   (error "GUD not properly entered."))
  62.  
  63. (defun gud-marker-filter (str)
  64.   (error "GUD not properly entered."))
  65.  
  66. (defun gud-find-file (f)
  67.   (error "GUD not properly entered."))
  68.  
  69. ;; ======================================================================
  70. ;; command definition
  71.  
  72. ;; This macro is used below to define some basic debugger interface commands.
  73. ;; Of course you may use `gud-def' with any other debugger command, including
  74. ;; user defined ones.
  75.  
  76. ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
  77. ;; which defines FUNC to send the command NAME to the debugger, gives
  78. ;; it the docstring DOC, and binds that function to KEY in the GUD
  79. ;; major mode.  The function is also bound in the global keymap with the
  80. ;; GUD prefix.
  81.  
  82. (defmacro gud-def (func cmd key &optional doc)
  83.   "Define FUNC to be a command sending STR and bound to KEY, with
  84. optional doc string DOC.  Certain %-escapes in the string arguments
  85. are interpreted specially if present.  These are:
  86.  
  87.   %f    name of current source file. 
  88.   %l    number of current source line
  89.   %e    text of the C lvalue or function-call expression surrounding point.
  90.   %a    text of the hexadecimal address surrounding point
  91.   %p    prefix argument to the command (if any) as a number
  92.  
  93.   The `current' source file is the file of the current buffer (if
  94. we're in a C file) or the source file current at the last break or
  95. step (if we're in the GUD buffer).
  96.   The `current' line is that of the current buffer (if we're in a
  97. source file) or the source line number at the last break or step (if
  98. we're in the GUD buffer)."
  99.   (list 'progn
  100.     (list 'defun func '(arg)
  101.           (or doc "")
  102.           '(interactive "p")
  103.           (list 'gud-call cmd 'arg))
  104.     (if key
  105.         (list 'define-key
  106.           '(current-local-map)
  107.           (concat "\C-c" key)
  108.           (list 'quote func)))
  109.     (if key
  110.         (list 'global-set-key
  111.           (list 'concat 'gud-key-prefix key)
  112.           (list 'quote func)))))
  113.  
  114. ;; Where gud-display-frame should put the debugging arrow.  This is
  115. ;; set by the marker-filter, which scans the debugger's output for
  116. ;; indications of the current program counter.
  117. (defvar gud-last-frame nil)
  118.  
  119. ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
  120. ;; the last frame, even if it's been called before and gud-last-frame has
  121. ;; been set to nil.
  122. (defvar gud-last-last-frame)
  123.  
  124. ;; All debugger-specific information is collected here.
  125. ;; Here's how it works, in case you ever need to add a debugger to the mode.
  126. ;;
  127. ;; Each entry must define the following at startup:
  128. ;;
  129. ;;<name>
  130. ;; comint-prompt-regexp
  131. ;; gud-<name>-debugger-startup
  132. ;; gud-<name>-marker-filter
  133. ;; gud-<name>-find-file
  134. ;;
  135. ;; The job of the startup-command method is to fire up a copy of the debugger,
  136. ;; given a list of debugger arguments.
  137. ;;
  138. ;; The job of the marker-filter method is to detect file/line markers in
  139. ;; strings and set the global gud-last-frame to indicate what display
  140. ;; action (if any) should be triggered by the marker.  Note that only
  141. ;; whatever the method *returns* is displayed in the buffer; thus, you
  142. ;; can filter the debugger's output, interpreting some and passing on
  143. ;; the rest.
  144. ;;
  145. ;; The job of the find-file method is to visit and return the buffer indicated
  146. ;; by the car of gud-tag-frame.  This may be a file name, a tag name, or
  147. ;; something else.
  148.  
  149. ;; ======================================================================
  150. ;; gdb functions
  151.  
  152. ;;; History of argument lists passed to gdb.
  153. (defvar gud-gdb-history nil)
  154.  
  155. (defun gud-gdb-debugger-startup (file args)
  156.   (apply 'make-comint (concat "gud-" file) "gdb" nil "-fullname" args))
  157.  
  158. (defun gud-gdb-marker-filter (string)
  159.   (if (string-match  "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n" string)
  160.       (progn
  161.     (setq gud-last-frame
  162.           (cons
  163.            (substring string (match-beginning 1) (match-end 1))
  164.            (string-to-int
  165.         (substring string (match-beginning 2) (match-end 2)))))
  166.     ;; this computation means the ^Z^Z-initiated marker in the
  167.     ;; input string is never emitted.
  168.     (concat
  169.      (substring string 0 (match-beginning 0))
  170.      (substring string (match-end 0))
  171.      ))
  172.     string))
  173.  
  174. (defun gud-gdb-find-file (f)
  175.   (find-file-noselect f))
  176.  
  177. ;;;###autoload
  178. (defun gdb (args)
  179.   "Run gdb on program FILE in buffer *gud-FILE*.
  180. The directory containing FILE becomes the initial working directory
  181. and source-file directory for your debugger."
  182.   (interactive
  183.    (list (read-from-minibuffer "Run gdb (like this): gdb "
  184.                    (if (consp gud-gdb-history)
  185.                    (car gud-gdb-history)
  186.                  "")
  187.                    nil nil
  188.                    '(gud-gdb-history . 1))))
  189.   (gud-overload-functions '((gud-debugger-startup . gud-gdb-debugger-startup)
  190.                 (gud-marker-filter    . gud-gdb-marker-filter)
  191.                 (gud-find-file        . gud-gdb-find-file)
  192.                 ))
  193.  
  194.   (gud-common-init args)
  195.  
  196.   (gud-def gud-break  "break %f:%l"  "\C-b" "Set breakpoint at current line.")
  197.   (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
  198.   (gud-def gud-remove "clear %l"     "\C-d" "Remove breakpoint at current line")
  199.   (gud-def gud-step   "step %p"      "\C-s" "Step one source line with display.")
  200.   (gud-def gud-stepi  "stepi %p"     "\C-i" "Step one instruction with display.")
  201.   (gud-def gud-next   "next %p"      "\C-n" "Step one line (skip functions).")
  202.   (gud-def gud-cont   "cont"         "\C-r" "Continue with display.")
  203.   (gud-def gud-finish "finish"       "\C-f" "Finish executing current function.")
  204.   (gud-def gud-up     "up %p"        "<" "Up N stack frames (numeric arg).")
  205.   (gud-def gud-down   "down %p"      ">" "Down N stack frames (numeric arg).")
  206.   (gud-def gud-print  "print %e"     "\C-p" "Evaluate C expression at point.")
  207.  
  208.   (setq comint-prompt-regexp "^(.*gdb[+]?) *")
  209.   (run-hooks 'gdb-mode-hook)
  210.   )
  211.  
  212.  
  213. ;; ======================================================================
  214. ;; sdb functions
  215.  
  216. ;;; History of argument lists passed to sdb.
  217. (defvar gud-sdb-history nil)
  218.  
  219. (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
  220.   "If nil, we're on a System V Release 4 and don't need the tags hack.")
  221.  
  222. (defvar gud-sdb-lastfile nil)
  223.  
  224. (defun gud-sdb-debugger-startup (file args)
  225.   (apply 'make-comint (concat "gud-" file) "sdb" nil args))
  226.  
  227. (defun gud-sdb-marker-filter (string)
  228.   (cond 
  229.    ;; System V Release 3.2 uses this format
  230.    ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
  231.             string)
  232.     (setq gud-last-frame
  233.       (cons
  234.        (substring string (match-beginning 2) (match-end 2))
  235.        (string-to-int 
  236.         (substring string (match-beginning 3) (match-end 3))))))
  237.    ;; System V Release 4.0 
  238.    ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
  239.                string)
  240.     (setq gud-sdb-lastfile
  241.       (substring string (match-beginning 2) (match-end 2))))
  242.    ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
  243.      (setq gud-last-frame
  244.            (cons
  245.         gud-sdb-lastfile
  246.         (string-to-int 
  247.          (substring string (match-beginning 1) (match-end 1))))))
  248.    (t 
  249.     (setq gud-sdb-lastfile nil)))
  250.   string)
  251.  
  252. (defun gud-sdb-find-file (f)
  253.   (if gud-sdb-needs-tags
  254.       (find-tag-noselect f)
  255.     (find-file-noselect f)))
  256.  
  257. ;;;###autoload
  258. (defun sdb (args)
  259.   "Run sdb on program FILE in buffer *gud-FILE*.
  260. The directory containing FILE becomes the initial working directory
  261. and source-file directory for your debugger."
  262.   (interactive
  263.    (list (read-from-minibuffer "Run sdb (like this): sdb "
  264.                    (if (consp gud-sdb-history)
  265.                    (car gud-sdb-history)
  266.                  "")
  267.                    nil nil
  268.                    '(gud-sdb-history . 1))))
  269.   (if (and gud-sdb-needs-tags
  270.        (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
  271.       (error "The sdb support requires a valid tags table to work."))
  272.   (gud-overload-functions '((gud-debugger-startup . gud-sdb-debugger-startup)
  273.                 (gud-marker-filter    . gud-sdb-marker-filter)
  274.                 (gud-find-file        . gud-sdb-find-file)
  275.                 ))
  276.  
  277.   (gud-common-init args)
  278.  
  279.   (gud-def gud-break  "%l b" "\C-b"   "Set breakpoint at current line.")
  280.   (gud-def gud-tbreak "%l c" "\C-t"   "Set temporary breakpoint at current line.")
  281.   (gud-def gud-remove "%l d" "\C-d"   "Remove breakpoint at current line")
  282.   (gud-def gud-step   "s %p" "\C-s"   "Step one source line with display.")
  283.   (gud-def gud-stepi  "i %p" "\C-i"   "Step one instruction with display.")
  284.   (gud-def gud-next   "S %p" "\C-n"   "Step one line (skip functions).")
  285.   (gud-def gud-cont   "c"    "\C-r"   "Continue with display.")
  286.   (gud-def gud-print  "%e/"  "\C-p"   "Evaluate C expression at point.")
  287.  
  288.   (setq comint-prompt-regexp  "\\(^\\|\n\\)\\*")
  289.   (run-hooks 'sdb-mode-hook)
  290.   )
  291.  
  292. ;; ======================================================================
  293. ;; dbx functions
  294.  
  295. ;;; History of argument lists passed to dbx.
  296. (defvar gud-dbx-history nil)
  297.  
  298. (defun gud-dbx-debugger-startup (file args)
  299.   (apply 'make-comint (concat "gud-" file) "dbx" nil args))
  300.  
  301. (defun gud-dbx-marker-filter (string)
  302.   (if (string-match
  303.        "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\"" string)
  304.       (setq gud-last-frame
  305.         (cons
  306.          (substring string (match-beginning 2) (match-end 2))
  307.          (string-to-int 
  308.           (substring string (match-beginning 1) (match-end 1))))))
  309.   string)
  310.  
  311. (defun gud-dbx-find-file (f)
  312.   (find-file-noselect f))
  313.  
  314. ;;;###autoload
  315. (defun dbx (args)
  316.   "Run dbx on program FILE in buffer *gud-FILE*.
  317. The directory containing FILE becomes the initial working directory
  318. and source-file directory for your debugger."
  319.   (interactive
  320.    (list (read-from-minibuffer "Run dbx (like this): dbx "
  321.                    (if (consp gud-dbx-history)
  322.                    (car gud-dbx-history)
  323.                  "")
  324.                    nil nil
  325.                    '(gud-dbx-history . 1))))
  326.   (gud-overload-functions '((gud-debugger-startup . gud-dbx-debugger-startup)
  327.                 (gud-marker-filter    . gud-dbx-marker-filter)
  328.                 (gud-find-file        . gud-dbx-find-file)
  329.                 ))
  330.  
  331.   (gud-common-init args)
  332.  
  333.   (gud-def gud-break  "stop at \"%f\":%l"
  334.                      "\C-b" "Set breakpoint at current line.")
  335.   (gud-def gud-remove "clear %l"  "\C-d" "Remove breakpoint at current line")
  336.   (gud-def gud-step   "step %p"      "\C-s" "Step one line with display.")
  337.   (gud-def gud-stepi  "stepi %p"  "\C-i" "Step one instruction with display.")
  338.   (gud-def gud-next   "next %p"      "\C-n" "Step one line (skip functions).")
  339.   (gud-def gud-cont   "cont"      "\C-r" "Continue with display.")
  340.   (gud-def gud-up     "up %p"      "<" "Up (numeric arg) stack frames.")
  341.   (gud-def gud-down   "down %p"      ">" "Down (numeric arg) stack frames.")
  342.   (gud-def gud-print  "print %e"  "\C-p" "Evaluate C expression at point.")
  343.  
  344.   (setq comint-prompt-regexp  "^[^)]*dbx) *")
  345.   (run-hooks 'dbx-mode-hook)
  346.   )
  347.  
  348. ;; ======================================================================
  349. ;; xdb (HP PARISC debugger) functions
  350.  
  351. ;;; History of argument lists passed to xdb.
  352. (defvar gud-xdb-history nil)
  353.  
  354. (defvar gud-xdb-directories nil
  355.   "*A list of directories that xdb should search for source code.
  356. If nil, only source files in the program directory
  357. will be known to xdb.
  358.  
  359. The file names should be absolute, or relative to the directory
  360. containing the executable being debugged.")
  361.  
  362. (defun gud-xdb-debugger-startup (file args)
  363.   (apply 'make-comint (concat "gud-" file) "xdb" nil
  364.          (append (let ((directories gud-xdb-directories)
  365.                        (result nil))
  366.                    (while directories
  367.                      (setq result (cons (car directories) (cons "-d" result)))
  368.                      (setq directories (cdr directories)))
  369.                    (nreverse result))
  370.                  args)))
  371.  
  372. (defun gud-xdb-file-name (f)
  373.   "Transform a relative pathname to a full pathname in xdb mode"
  374.   (let ((result nil))
  375.     (if (file-exists-p f)
  376.         (setq result (expand-file-name f))
  377.       (let ((directories gud-xdb-directories))
  378.         (while directories
  379.           (let ((path (concat (car directories) "/" f)))
  380.             (if (file-exists-p path)
  381.                 (setq result (expand-file-name path)
  382.                       directories nil)))
  383.           (setq directories (cdr directories)))))
  384.     result))
  385.  
  386. ;; xdb does not print the lines all at once, so we have to accumulate them
  387. (defvar gud-xdb-accumulation "")
  388.  
  389. (defun gud-xdb-marker-filter (string)
  390.   (let (result)
  391.     (if (or (string-match comint-prompt-regexp string)
  392.             (string-match ".*\012" string))
  393.         (setq result (concat gud-xdb-accumulation string)
  394.               gud-xdb-accumulation "")
  395.       (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
  396.     (if result
  397.         (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
  398.                 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
  399.                               result))
  400.             (let ((line (string-to-int 
  401.                          (substring result (match-beginning 2) (match-end 2))))
  402.                   (file (gud-xdb-file-name
  403.                          (substring result (match-beginning 1) (match-end 1)))))
  404.               (if file
  405.                   (setq gud-last-frame (cons file line))))))
  406.     (or result "")))    
  407.                
  408. (defun gud-xdb-find-file (f)
  409.   (let ((realf (gud-xdb-file-name f)))
  410.     (if realf (find-file-noselect realf))))
  411.  
  412. ;;;###autoload
  413. (defun xdb (args)
  414.   "Run xdb on program FILE in buffer *gud-FILE*.
  415. The directory containing FILE becomes the initial working directory
  416. and source-file directory for your debugger.
  417.  
  418. You can set the variable 'gud-xdb-directories' to a list of program source
  419. directories if your program contains sources from more than one directory."
  420.   (interactive
  421.    (list (read-from-minibuffer "Run xdb (like this): xdb "
  422.                    (if (consp gud-xdb-history)
  423.                    (car gud-xdb-history)
  424.                  "")
  425.                    nil nil
  426.                    '(gud-xdb-history . 1))))
  427.   (gud-overload-functions '((gud-debugger-startup . gud-xdb-debugger-startup)
  428.                 (gud-marker-filter    . gud-xdb-marker-filter)
  429.                 (gud-find-file        . gud-xdb-find-file)))
  430.  
  431.   (gud-common-init args)
  432.  
  433.   (gud-def gud-break  "b %f:%l"    "\C-b" "Set breakpoint at current line.")
  434.   (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
  435.            "Set temporary breakpoint at current line.")
  436.   (gud-def gud-remove "db"         "\C-d" "Remove breakpoint at current line")
  437.   (gud-def gud-step   "s %p"       "\C-s" "Step one line with display.")
  438.   (gud-def gud-next   "S %p"       "\C-n" "Step one line (skip functions).")
  439.   (gud-def gud-cont   "c"       "\C-r" "Continue with display.")
  440.   (gud-def gud-up     "up %p"       "<"    "Up (numeric arg) stack frames.")
  441.   (gud-def gud-down   "down %p"       ">"    "Down (numeric arg) stack frames.")
  442.   (gud-def gud-finish "bu\\t"      "\C-f" "Finish executing current function.")
  443.   (gud-def gud-print  "p %e"       "\C-p" "Evaluate C expression at point.")
  444.  
  445.   (setq comint-prompt-regexp  "^>")
  446.   (make-local-variable 'gud-xdb-accumulation)
  447.   (setq gud-xdb-accumulation "")
  448.   (run-hooks 'xdb-mode-hook))
  449.  
  450. ;;
  451. ;; End of debugger-specific information
  452. ;;
  453.  
  454. ;;; When we send a command to the debugger via gud-call, it's annoying
  455. ;;; to see the command and the new prompt inserted into the debugger's
  456. ;;; buffer; we have other ways of knowing the command has completed.
  457. ;;;
  458. ;;; If the buffer looks like this:
  459. ;;; --------------------
  460. ;;; (gdb) set args foo bar
  461. ;;; (gdb) -!-
  462. ;;; --------------------
  463. ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
  464. ;;; source file to set a breakpoint, we want the buffer to end up like
  465. ;;; this:
  466. ;;; --------------------
  467. ;;; (gdb) set args foo bar
  468. ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
  469. ;;; (gdb) -!-
  470. ;;; --------------------
  471. ;;; Essentially, the old prompt is deleted, and the command's output
  472. ;;; and the new prompt take its place.
  473. ;;;
  474. ;;; Not echoing the command is easy enough; you send it directly using
  475. ;;; process-send-string, and it never enters the buffer.  However,
  476. ;;; getting rid of the old prompt is trickier; you don't want to do it
  477. ;;; when you send the command, since that will result in an annoying
  478. ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
  479. ;;; waits for a response from the debugger, and the new prompt is
  480. ;;; inserted.  Instead, we'll wait until we actually get some output
  481. ;;; from the subprocess before we delete the prompt.  If the command
  482. ;;; produced no output other than a new prompt, that prompt will most
  483. ;;; likely be in the first chunk of output received, so we will delete
  484. ;;; the prompt and then replace it with an identical one.  If the
  485. ;;; command produces output, the prompt is moving anyway, so the
  486. ;;; flicker won't be annoying.
  487. ;;;
  488. ;;; So - when we want to delete the prompt upon receipt of the next
  489. ;;; chunk of debugger output, we position gud-delete-prompt-marker at
  490. ;;; the start of the prompt; the process filter will notice this, and
  491. ;;; delete all text between it and the process output marker.  If
  492. ;;; gud-delete-prompt-marker points nowhere, we leave the current
  493. ;;; prompt alone.
  494. (defvar gud-delete-prompt-marker nil)
  495.  
  496.  
  497. (defun gud-mode ()
  498.   "Major mode for interacting with an inferior debugger process.
  499.  
  500.    You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
  501. or M-x xdb.  Each entry point finishes by executing a hook; `gdb-mode-hook',
  502. `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
  503.  
  504. After startup, the following commands are available in both the GUD
  505. interaction buffer and any source buffer GUD visits due to a breakpoint stop
  506. or step operation:
  507.  
  508. \\[gud-break] sets a breakpoint at the current file and line.  In the
  509. GUD buffer, the current file and line are those of the last breakpoint or
  510. step.  In a source buffer, they are the buffer's file and current line.
  511.  
  512. \\[gud-remove] removes breakpoints on the current file and line.
  513.  
  514. \\[gud-refresh] displays in the source window the last line referred to
  515. in the gud buffer.
  516.  
  517. \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
  518. step-one-line (not entering function calls), and step-one-instruction
  519. and then update the source window with the current file and position.
  520. \\[gud-cont] continues execution.
  521.  
  522. \\[gud-print] tries to find the largest C lvalue or function-call expression
  523. around point, and sends it to the debugger for value display.
  524.  
  525. The above commands are common to all supported debuggers except xdb which
  526. does not support stepping instructions.
  527.  
  528. Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
  529. except that the breakpoint is temporary; that is, it is removed when
  530. execution stops on it.
  531.  
  532. Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
  533. frame.  \\[gud-down] drops back down through one.
  534.  
  535. If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
  536. the current function and stops.
  537.  
  538. All the keystrokes above are accessible in the GUD buffer
  539. with the prefix C-c, and in all buffers through the prefix C-x C-a.
  540.  
  541. All pre-defined functions for which the concept make sense repeat
  542. themselves the appropriate number of times if you give a prefix
  543. argument.
  544.  
  545. You may use the `gud-def' macro in the initialization hook to define other
  546. commands.
  547.  
  548. Other commands for interacting with the debugger process are inherited from
  549. comint mode, which see."
  550.   (interactive)
  551.   (comint-mode)
  552.   (setq major-mode 'gud-mode)
  553.   (setq mode-name "Debugger")
  554.   (setq mode-line-process '(": %s"))
  555.   (use-local-map (copy-keymap comint-mode-map))
  556.   (make-local-variable 'gud-last-frame)
  557.   (setq gud-last-frame nil)
  558.   (make-local-variable 'comint-prompt-regexp)
  559.   (make-local-variable 'gud-delete-prompt-marker)
  560.   (setq gud-delete-prompt-marker (make-marker))
  561.   (run-hooks 'gud-mode-hook)
  562. )
  563.  
  564. (defvar gud-comint-buffer nil)
  565.  
  566. (defun gud-common-init (args)
  567.   ;; Perform initializations common to all debuggers
  568.   ;; There *must* be a cleaner way to lex the arglist...
  569.   (let (file i)
  570.     (if (string= args "")
  571.     (setq args nil)
  572.       (save-excursion
  573.     (set-buffer (get-buffer-create "*gud-scratch*"))
  574.     (erase-buffer)
  575.     (insert args)
  576.     (goto-char (point-max))
  577.     (insert "\")")
  578.     (goto-char (point-min))
  579.     (insert "(\"")
  580.     (while (re-search-forward " +" nil t)
  581.       (replace-match "\" \"" nil nil))
  582.     (goto-char (point-min))
  583.     (while (re-search-forward "\"\"" nil t)
  584.       (replace-match "" nil nil))
  585.     (setq args (read (buffer-string)))
  586.     (kill-buffer (current-buffer))))
  587.     (setq i (1- (length args)))
  588.     (while (and (>= i 0) (not (= (aref (nth i args) 0) ?-)))
  589.       (setq file (nth i args)) (setq i (1- i)))
  590.     (let* ((path (expand-file-name file))
  591.        (filepart (file-name-nondirectory path)))
  592.       (switch-to-buffer (concat "*gud-" filepart "*"))
  593.       (setq default-directory (file-name-directory path))
  594.       (or (bolp) (newline))
  595.       (insert "Current directory is " default-directory "\n")
  596.       (gud-debugger-startup filepart args)))
  597.   (gud-mode)
  598.   (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
  599.   (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
  600.   (gud-set-buffer)
  601.   )
  602.  
  603. (defun gud-set-buffer ()
  604.   (cond ((eq major-mode 'gud-mode)
  605.     (setq gud-comint-buffer (current-buffer)))))
  606.  
  607. ;; These functions are responsible for inserting output from your debugger
  608. ;; into the buffer.  The hard work is done by the method that is
  609. ;; the value of gud-marker-filter.
  610.  
  611. (defun gud-filter (proc string)
  612.   ;; Here's where the actual buffer insertion is done
  613.   (let ((inhibit-quit t))
  614.     (save-excursion
  615.       (set-buffer (process-buffer proc))
  616.       (let (moving output-after-point)
  617.     (save-excursion
  618.       (goto-char (process-mark proc))
  619.       ;; If we have been so requested, delete the debugger prompt.
  620.       (if (marker-buffer gud-delete-prompt-marker)
  621.           (progn
  622.         (delete-region (point) gud-delete-prompt-marker)
  623.         (set-marker gud-delete-prompt-marker nil)))
  624.       (insert-before-markers (gud-marker-filter string))
  625.       (setq moving (= (point) (process-mark proc)))
  626.       (setq output-after-point (< (point) (process-mark proc)))
  627.       ;; Check for a filename-and-line number.
  628.       ;; Don't display the specified file
  629.       ;; unless (1) point is at or after the position where output appears
  630.       ;; and (2) this buffer is on the screen.
  631.       (if (and gud-last-frame
  632.            (not output-after-point)
  633.            (get-buffer-window (current-buffer)))
  634.           (gud-display-frame)))
  635.     (if moving (goto-char (process-mark proc)))))))
  636.  
  637. (defun gud-sentinel (proc msg)
  638.   (cond ((null (buffer-name (process-buffer proc)))
  639.      ;; buffer killed
  640.      ;; Stop displaying an arrow in a source file.
  641.      (setq overlay-arrow-position nil)
  642.      (set-process-buffer proc nil))
  643.     ((memq (process-status proc) '(signal exit))
  644.      ;; Stop displaying an arrow in a source file.
  645.      (setq overlay-arrow-position nil)
  646.      ;; Fix the mode line.
  647.      (setq mode-line-process
  648.            (concat ": "
  649.                (symbol-name (process-status proc))))
  650.      (let* ((obuf (current-buffer)))
  651.        ;; save-excursion isn't the right thing if
  652.        ;;  process-buffer is current-buffer
  653.        (unwind-protect
  654.            (progn
  655.          ;; Write something in *compilation* and hack its mode line,
  656.          (set-buffer (process-buffer proc))
  657.          ;; Force mode line redisplay soon
  658.          (set-buffer-modified-p (buffer-modified-p))
  659.          (if (eobp)
  660.              (insert ?\n mode-name " " msg)
  661.            (save-excursion
  662.              (goto-char (point-max))
  663.              (insert ?\n mode-name " " msg)))
  664.          ;; If buffer and mode line will show that the process
  665.          ;; is dead, we can delete it now.  Otherwise it
  666.          ;; will stay around until M-x list-processes.
  667.          (delete-process proc))
  668.          ;; Restore old buffer, but don't restore old point
  669.          ;; if obuf is the gud buffer.
  670.          (set-buffer obuf))))))
  671.  
  672. (defun gud-display-frame ()
  673.   "Find and obey the last filename-and-line marker from the debugger.
  674. Obeying it means displaying in another window the specified file and line."
  675.   (interactive)
  676.   (if gud-last-frame
  677.    (progn
  678.      (gud-set-buffer)
  679.      (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
  680.      (setq gud-last-last-frame gud-last-frame
  681.        gud-last-frame nil))))
  682.  
  683. ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
  684. ;; and that its line LINE is visible.
  685. ;; Put the overlay-arrow on the line LINE in that buffer.
  686. ;; Most of the trickiness in here comes from wanting to preserve the current
  687. ;; region-restriction if that's possible.  We use an explicit display-buffer
  688. ;; to get around the fact that this is called inside a save-excursion.
  689.  
  690. (defun gud-display-line (true-file line)
  691.   (let* ((buffer (gud-find-file true-file))
  692.      (window (display-buffer buffer))
  693.      (pos))
  694. ;;;    (if (equal buffer (current-buffer))
  695. ;;;    nil
  696. ;;;      (setq buffer-read-only nil))
  697.     (save-excursion
  698. ;;;      (setq buffer-read-only t)
  699.       (set-buffer buffer)
  700.       (save-restriction
  701.     (widen)
  702.     (goto-line line)
  703.     (setq pos (point))
  704.     (setq overlay-arrow-string "=>")
  705.     (or overlay-arrow-position
  706.         (setq overlay-arrow-position (make-marker)))
  707.     (set-marker overlay-arrow-position (point) (current-buffer)))
  708.       (cond ((or (< pos (point-min)) (> pos (point-max)))
  709.          (widen)
  710.          (goto-char pos))))
  711.     (set-window-point window overlay-arrow-position)))
  712.  
  713. ;;; The gud-call function must do the right thing whether its invoking
  714. ;;; keystroke is from the GUD buffer itself (via major-mode binding)
  715. ;;; or a C buffer.  In the former case, we want to supply data from
  716. ;;; gud-last-frame.  Here's how we do it:
  717.  
  718. (defun gud-format-command (str arg)
  719.   (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
  720.     (if (string-match "\\(.*\\)%f\\(.*\\)" str)
  721.     (progn
  722.       (setq str (concat
  723.              (substring str (match-beginning 1) (match-end 1))
  724.              (file-name-nondirectory (if insource
  725.                          (buffer-file-name)
  726.                            (car gud-last-frame)))
  727.              (substring str (match-beginning 2) (match-end 2))))))
  728.     (if (string-match "\\(.*\\)%l\\(.*\\)" str)
  729.     (progn
  730.       (setq str (concat
  731.              (substring str (match-beginning 1) (match-end 1))
  732.              (if insource
  733.              (save-excursion
  734.                (beginning-of-line)
  735.                (save-restriction (widen) 
  736.                          (1+ (count-lines 1 (point)))))
  737.                (cdr gud-last-frame))
  738.              (substring str (match-beginning 2) (match-end 2))))))
  739.     (if (string-match "\\(.*\\)%e\\(.*\\)" str)
  740.     (progn
  741.       (setq str (concat
  742.              (substring str (match-beginning 1) (match-end 1))
  743.              (find-c-expr)
  744.              (substring str (match-beginning 2) (match-end 2))))))
  745.     (if (string-match "\\(.*\\)%a\\(.*\\)" str)
  746.     (progn
  747.       (setq str (concat
  748.              (substring str (match-beginning 1) (match-end 1))
  749.              (gud-read-address)
  750.              (substring str (match-beginning 2) (match-end 2))))))
  751.     (if (string-match "\\(.*\\)%p\\(.*\\)" str)
  752.     (progn
  753.       (setq str (concat
  754.              (substring str (match-beginning 1) (match-end 1))
  755.              (if arg (int-to-string arg) "")
  756.              (substring str (match-beginning 2) (match-end 2))))))
  757.     )
  758.   str
  759.   )
  760.  
  761. (defun gud-read-address ()
  762.   "Return a string containing the core-address found in the buffer at point."
  763.   (save-excursion
  764.     (let ((pt (point)) found begin)
  765.       (setq found (if (search-backward "0x" (- pt 7) t) (point)))
  766.       (cond
  767.        (found (forward-char 2)
  768.           (buffer-substring found
  769.                 (progn (re-search-forward "[^0-9a-f]")
  770.                        (forward-char -1)
  771.                        (point))))
  772.        (t (setq begin (progn (re-search-backward "[^0-9]") 
  773.                  (forward-char 1)
  774.                  (point)))
  775.       (forward-char 1)
  776.       (re-search-forward "[^0-9]")
  777.       (forward-char -1)
  778.       (buffer-substring begin (point)))))))
  779.  
  780. (defun gud-call (fmt &optional arg)
  781.   (let ((msg (gud-format-command fmt arg)))
  782.     (message "Command: %s" msg)
  783.     (sit-for 0)
  784.     (gud-basic-call msg)))
  785.  
  786. (defun gud-basic-call (command)
  787.   "Invoke the debugger COMMAND displaying source in other window."
  788.   (interactive)
  789.   (gud-set-buffer)
  790.   (let ((command (concat command "\n"))
  791.     (proc (get-buffer-process gud-comint-buffer)))
  792.  
  793.     ;; Arrange for the current prompt to get deleted.
  794.     (save-excursion
  795.       (set-buffer gud-comint-buffer)
  796.       (goto-char (process-mark proc))
  797.       (beginning-of-line)
  798.       (if (looking-at comint-prompt-regexp)
  799.       (set-marker gud-delete-prompt-marker (point))))
  800.     (process-send-string proc command)))
  801.  
  802. (defun gud-refresh (&optional arg)
  803.   "Fix up a possibly garbled display, and redraw the arrow."
  804.   (interactive "P")
  805.   (recenter arg)
  806.   (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
  807.   (gud-display-frame))
  808.  
  809. ;;; Code for parsing expressions out of C code.  The single entry point is
  810. ;;; find-c-expr, which tries to return an lvalue expression from around point.
  811. ;;;
  812. ;;; The rest of this file is a hacked version of gdbsrc.el by
  813. ;;; Debby Ayers <ayers@asc.slb.com>,
  814. ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
  815.  
  816. (defun find-c-expr ()
  817.   "Returns the C expr that surrounds point."
  818.   (interactive)
  819.   (save-excursion
  820.     (let ((p) (expr) (test-expr))
  821.       (setq p (point))
  822.       (setq expr (expr-cur))
  823.       (setq test-expr (expr-prev))
  824.       (while (expr-compound test-expr expr)
  825.     (setq expr (cons (car test-expr) (cdr expr)))
  826.     (goto-char (car expr))
  827.     (setq test-expr (expr-prev)))
  828.       (goto-char p)
  829.       (setq test-expr (expr-next))
  830.       (while (expr-compound expr test-expr)
  831.     (setq expr (cons (car expr) (cdr test-expr)))
  832.     (setq test-expr (expr-next))
  833.     )
  834.       (buffer-substring (car expr) (cdr expr)))))
  835.  
  836. (defun expr-cur ()
  837.   "Returns the expr that point is in; point is set to beginning of expr.
  838. The expr is represented as a cons cell, where the car specifies the point in
  839. the current buffer that marks the beginning of the expr and the cdr specifies 
  840. the character after the end of the expr."
  841.   (let ((p (point)) (begin) (end))
  842.     (expr-backward-sexp)
  843.     (setq begin (point))
  844.     (expr-forward-sexp)
  845.     (setq end (point))
  846.     (if (>= p end) 
  847.     (progn
  848.      (setq begin p)
  849.      (goto-char p)
  850.      (expr-forward-sexp)
  851.      (setq end (point))
  852.      )
  853.       )
  854.     (goto-char begin)
  855.     (cons begin end)))
  856.  
  857. (defun expr-backward-sexp ()
  858.   "Version of `backward-sexp' that catches errors."
  859.   (condition-case nil
  860.       (backward-sexp)
  861.     (error t)))
  862.  
  863. (defun expr-forward-sexp ()
  864.   "Version of `forward-sexp' that catches errors."
  865.   (condition-case nil
  866.      (forward-sexp)
  867.     (error t)))
  868.  
  869. (defun expr-prev ()
  870.   "Returns the previous expr, point is set to beginning of that expr.
  871. The expr is represented as a cons cell, where the car specifies the point in
  872. the current buffer that marks the beginning of the expr and the cdr specifies 
  873. the character after the end of the expr"
  874.   (let ((begin) (end))
  875.     (expr-backward-sexp)
  876.     (setq begin (point))
  877.     (expr-forward-sexp)
  878.     (setq end (point))
  879.     (goto-char begin)
  880.     (cons begin end)))
  881.  
  882. (defun expr-next ()
  883.   "Returns the following expr, point is set to beginning of that expr.
  884. The expr is represented as a cons cell, where the car specifies the point in
  885. the current buffer that marks the beginning of the expr and the cdr specifies 
  886. the character after the end of the expr."
  887.   (let ((begin) (end))
  888.     (expr-forward-sexp)
  889.     (expr-forward-sexp)
  890.     (setq end (point))
  891.     (expr-backward-sexp)
  892.     (setq begin (point))
  893.     (cons begin end)))
  894.  
  895. (defun expr-compound-sep (span-start span-end)
  896.   "Returns '.' for '->' & '.', returns ' ' for white space,
  897. returns '?' for other punctuation."
  898.   (let ((result ? )
  899.     (syntax))
  900.     (while (< span-start span-end)
  901.       (setq syntax (char-syntax (char-after span-start)))
  902.       (cond
  903.        ((= syntax ? ) t)
  904.        ((= syntax ?.) (setq syntax (char-after span-start))
  905.     (cond 
  906.      ((= syntax ?.) (setq result ?.))
  907.      ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
  908.       (setq result ?.)
  909.       (setq span-start (+ span-start 1)))
  910.      (t (setq span-start span-end)
  911.         (setq result ??)))))
  912.       (setq span-start (+ span-start 1)))
  913.     result))
  914.  
  915. (defun expr-compound (first second)
  916.   "Non-nil if concatenating FIRST and SECOND makes a single C token.
  917. The two exprs are represented as a cons cells, where the car 
  918. specifies the point in the current buffer that marks the beginning of the 
  919. expr and the cdr specifies the character after the end of the expr.
  920. Link exprs of the form:
  921.       Expr -> Expr
  922.       Expr . Expr
  923.       Expr (Expr)
  924.       Expr [Expr]
  925.       (Expr) Expr
  926.       [Expr] Expr"
  927.   (let ((span-start (cdr first))
  928.     (span-end (car second))
  929.     (syntax))
  930.     (setq syntax (expr-compound-sep span-start span-end))
  931.     (cond
  932.      ((= (car first) (car second)) nil)
  933.      ((= (cdr first) (cdr second)) nil)
  934.      ((= syntax ?.) t)
  935.      ((= syntax ? )
  936.      (setq span-start (char-after (- span-start 1)))
  937.      (setq span-end (char-after span-end))
  938.      (cond
  939.       ((= span-start ?) ) t )
  940.       ((= span-start ?] ) t )
  941.           ((= span-end ?( ) t )
  942.       ((= span-end ?[ ) t )
  943.       (t nil))
  944.      )
  945.      (t nil))))
  946.  
  947. ;;; gud.el ends here
  948.